package com.ruoqing.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoqing.blog.dao.BlogIntroduceDao;
import com.ruoqing.blog.dao.RuoXiBlogContentDao;
import com.ruoqing.blog.dao.RuoXiBlogDetailsDao;
import com.ruoqing.blog.service.api.BlogService;
import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.domain.rqblog.BlogIntroducePo;
import com.ruoqing.core.domain.rqblog.RuoXiBlogContentPo;
import com.ruoqing.core.domain.rqblog.RuoXiBlogDetailsPo;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.blog.BlogContentDto;
import com.ruoqing.core.dto.blog.BlogDetailsDto;
import com.ruoqing.core.dto.blog.BlogPageDto;
import com.ruoqing.core.pojo.BlogDetailsPojo;
import com.ruoqing.core.vo.blog.BlogDetailsVo;
import com.ruoqing.utils.common.BeanSetterUtils;
import com.ruoqing.utils.common.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
	* @author YaoXian
	* @title: BlogServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 博客基础操作实现类
	* @date 2021-01-20 12:41
	*/
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
		
		@Resource
		private BlogIntroduceDao blogIntroduceDao;
		
		@Resource
		private RuoXiBlogDetailsDao ruoXiBlogDetailsDao;
		
		@Resource
		private RuoXiBlogContentDao ruoXiBlogContentDao;
		
		public BlogPageDto selectBlogByPage(Integer currentPage, Integer pageSize) {
				QueryWrapper<BlogIntroducePo> queryWrapper = new QueryWrapper<BlogIntroducePo>();
				Page<BlogIntroducePo> page = new Page<BlogIntroducePo>(currentPage, pageSize);
				Page<BlogIntroducePo> blogPage = blogIntroduceDao.selectPage(page, queryWrapper);
				BlogPageDto blogPageDto = new BlogPageDto();
				BeanSetterUtils.setPageAttributes(blogPageDto,blogPage);
				blogPageDto.setList(blogPage.getRecords());
				return blogPageDto;
		}
		
		@Transactional(rollbackFor = Exception.class)
		public BlogDetailsVo showDetailsById(Integer detailId) {
				if(org.springframework.util.StringUtils.isEmpty(detailId) || detailId < 0){
						return (BlogDetailsVo) BaseResponse.buildResponse(Constants.responseError,"文章不存在!");
				}
				BlogDetailsDto blogDetailsDto = new BlogDetailsDto();
				RuoXiBlogDetailsPo blogDetailsPo = ruoXiBlogDetailsDao.selectById(detailId);
				blogDetailsDto.setPraiseCount(blogDetailsPo.getPraiseCount());
				blogDetailsDto.setDislikeCount(blogDetailsPo.getDislikeCount());
				blogDetailsDto.setLastTime(blogDetailsPo.getLastTime());
				blogDetailsDto.setTitle(blogDetailsPo.getTitle());
				String[] contentIds = StringUtils.split(blogDetailsPo.getContentIds(), ",");
				if (ObjectUtils.isEmpty(contentIds)){
						log.error("博客内容为空");
						throw new RuntimeException("博客内容为空");
				}
				ArrayList<BlogContentDto> blogContentsList = new ArrayList<BlogContentDto>(contentIds.length);
				for (String contentId : contentIds) {
						RuoXiBlogContentPo blogContentPo = ruoXiBlogContentDao
										.selectById(Integer.parseInt(contentId));
						BlogContentDto blogContentDto = new BlogContentDto();
						BlogDetailsPojo blogDetailsPojo = new BlogDetailsPojo();
						blogContentDto.setId(blogContentPo.getId());
						blogDetailsPojo.setEntry(blogContentPo.getEntry());
						blogDetailsPojo.setType(blogContentPo.getType());
						if ("classify".equals(blogContentPo.getType())){
								String[] classify = StringUtils.split(blogContentPo.getContent(), "丨");
								ArrayList<String> classifyList = new ArrayList<String>(classify.length);
								classifyList.addAll(Arrays.asList(classify));
								blogContentDto.setClassifyList(classifyList);
						} else if ("entry_classify".equals(blogContentPo.getType())){
								String[] entryClassify = StringUtils.split(blogContentPo.getContent(), ",");
								List<RuoXiBlogContentPo> entryClassifyList =
												ruoXiBlogContentDao.selectBatchIds(Arrays.asList(entryClassify));
								ArrayList<BlogDetailsPojo> blogDetailsPojoList =
												new ArrayList<BlogDetailsPojo>(entryClassifyList.size());
								for (RuoXiBlogContentPo entryPo : entryClassifyList) {
										BlogDetailsPojo blogEntryPojo = new BlogDetailsPojo();
										blogEntryPojo.setEntry(entryPo.getEntry());
										blogEntryPojo.setContent(entryPo.getContent());
										blogEntryPojo.setType(entryPo.getType());
										blogDetailsPojoList.add(blogEntryPojo);
								}
								blogContentDto.setEntryClassifyList(blogDetailsPojoList);
						} else {
								blogDetailsPojo.setContent(blogContentPo.getContent());
						}
						blogContentDto.setBlogDetailsPojo(blogDetailsPojo);
						if (blogContentPo.getContain()){
								RuoXiBlogContentPo containContentPo = ruoXiBlogContentDao.
												selectById(blogContentPo.getContainId());
								blogContentDto.setIsContain(true);
								blogContentDto.setContainContent(containContentPo.getContent());
								blogContentDto.setContainType(containContentPo.getType());
						}
						blogContentsList.add(blogContentDto);
				}
				blogDetailsDto.setContentDtoList(blogContentsList);
				Integer viewCount = blogDetailsPo.getViewCount() + 1;
				blogDetailsDto.setViewCount(viewCount);
				ruoXiBlogDetailsDao.updateViewCountById(viewCount, detailId);
				blogIntroduceDao.updateViewCountById(viewCount,detailId);
				return BlogDetailsVo.buildBlogDetailsVo(Constants.responseOk,blogDetailsDto);
		}
		
		@Transactional(rollbackFor = Exception.class)
		public BaseResponse thumbBlogById(Integer blogId,String type) {
				String msg;
				RuoXiBlogDetailsPo blogDetailsPo = ruoXiBlogDetailsDao.selectById(blogId);
				if ("praise".equals(type)){
						ruoXiBlogDetailsDao.updatePraiseCountById(blogDetailsPo.getPraiseCount()+1,blogId);
						msg = "点赞成功!";
				}else if ("dislike".equals(type)){
						ruoXiBlogDetailsDao.updateDislikeCountById(blogDetailsPo.getDislikeCount()+1,blogId);
						msg = "踩一下成功!";
				}else {
						msg = "错误类型!";
						return BaseResponse.buildResponse(Constants.responseError,msg);
				}
				return BaseResponse.buildResponse(Constants.responseOk,msg);
		}
}
